package com.seocoo.worker.user.account;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.widget.TextView;

import com.seocoo.worker.R;
import com.seocoo.worker.base.Const;
import com.seocoo.worker.base.UtilStatus;
import com.seocoo.worker.base.entity.ELogin;
import com.seocoo.worker.base.widget.AppToolBer;
import com.seocoo.worker.base.widget.LtvRetLayout;
import com.seocoo.worker.user.login.model.ReqSMS;
import com.seocoo.worker.user.account.model.CardModel;
import com.seocoo.worker.user.account.model.req.ReqBundCard;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMActivity;
import com.sfh.lib.utils.ThreadTaskUtils;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/***
 * 添加银行卡
 */
public class AddBankCarActivity extends MVVMActivity {

    private AppToolBer appToolBer;
    private LtvRetLayout leBank;
    private LtvRetLayout leName;
    private LtvRetLayout leNumber;
    private LtvRetLayout leCode;
    private TextView tvCode;
    private TextView tvOk;

    public static void start(Activity context,int requestCode) {
        Intent intent = new Intent(context, AddBankCarActivity.class);
        context.startActivityForResult(intent,requestCode);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_add_bankcar_activity);
        UtilStatus.initStatus(this, Color.WHITE);
        initView();
    }


    private void initView() {
        appToolBer = (AppToolBer) findViewById(R.id.appToolBer);
        leBank = (LtvRetLayout) findViewById(R.id.le_bank);
        leName = (LtvRetLayout) findViewById(R.id.le_name);
        leNumber = (LtvRetLayout) findViewById(R.id.le_number);
        leCode = (LtvRetLayout) findViewById(R.id.le_code);
        tvCode = (TextView) findViewById(R.id.tv_code);
        tvOk = (TextView) findViewById(R.id.tv_ok);

        leNumber.getEtRight().setInputType(InputType.TYPE_CLASS_NUMBER);
        tvCode.setOnClickListener(v -> {
            //3:银行卡绑定-验证码
            ELogin login = AppCacheManager.getCacheListener().getObject(Const.LOGIN_KEY, ELogin.class);
            getViewModel(CardModel.class).sendSMS(new ReqSMS(login.getUserInfo().getPhoneNumber(), "3"));
        });
        tvOk.setOnClickListener(v -> {
            ReqBundCard bundCard = new ReqBundCard();

            if (TextUtils.isEmpty(leBank.getContent())) {
                showToast("请输入开户行名称");
                return;
            }

            if (TextUtils.isEmpty(leName.getContent())) {
                showToast("请输入持卡人姓名");
                return;
            }

            if (TextUtils.isEmpty(leNumber.getContent())) {
                showToast("请输入银行卡号");
                return;
            }

            if (TextUtils.isEmpty(leCode.getContent())) {
                showToast("请输入验证码");
                return;
            }
            bundCard.cardNo = leNumber.getContent();
            bundCard.cardName = leName.getContent();
            bundCard.bankName = leBank.getContent();
            bundCard.smsCode = leCode.getContent();

            getViewModel(CardModel.class).bundCard(bundCard);
        });
    }

    @LiveDataMatch
    public void onBundCardSuccess() {

        showToast("绑定银行卡成功！");
        setResult(Activity.RESULT_OK);
        finish();
    }

    private int sizeCount;
    private Future sizeFuture;

    @LiveDataMatch
    public void onSendCodeSuccess() {

        //发验证码
        sizeCount = 60;
        if (sizeFuture != null && !sizeFuture.isCancelled()) {
            sizeFuture.cancel(true);
            sizeFuture = null;
        }
        sizeFuture = ThreadTaskUtils.scheduleWithFixedDelay(() -> {
            runOnUiThread(() -> {
                sizeCount--;
                if (sizeCount > 0) {
                    tvCode.setEnabled(false);
                    tvCode.setText(String.format("%sS", sizeCount));

                } else {
                    sizeFuture.cancel(true);
                    tvCode.setEnabled(true);
                    tvCode.setText("获取验证码");
                }
            });
        }, 1000, 1000, TimeUnit.MILLISECONDS);
        this.putFuture(sizeFuture);


    }

}
